//支付方式点击事件
$('.payWay').on('click', function () {
    payType();
    $(this).attr('class', 'payWay on');

});
function payType() {
    $('.payWay').attr('class', 'payWay');
}
//点击进入影片后，开始倒计时
var time = 15;
var canStart = true; //是否可以获取总分钟
var canInterval = true; //是否可以运行定时器
window.onload = function () {
    if (canStart) {
        //总秒数
        totalSeconds = time * 60;
        canStart = false;
    }
    if (canInterval) {
        timer = setInterval(() => {
            // 将时间显示在页面上，并且每一次都要秒数-1
            if (totalSeconds > 0) {
                totalSeconds--;
            } else {
                // 倒计时结束，暂停定时器
                pause();
            }
            var minutes = parseInt((totalSeconds % 3600) / 60);
            var seconds = totalSeconds % 60;
            //显示在页面上
            $(".minute").text(minutes);
            $(".second").text(seconds);
        }, 1000);
        canInterval = false;
    }
    render();

    

}
function pause() {
    //点击确认支付后，暂停倒计时
    clearInterval(timer);
    canInterval = true;

    let loginUser = localStorage.getItem("loginUser");
    let order = localStorage.getItem("order");
    loginUser = JSON.parse(loginUser);
    order = JSON.parse(order);
    let orderAll;
    if (location.search) {
        orderAll = loginUser.order.filter(item => item.orderId != location.search.substr(4));
    }else{
        orderAll = loginUser.order.filter(item => item.orderId != order.orderId);
    }
    loginUser.order = orderAll;
    localStorage.setItem("loginUser",JSON.stringify(loginUser));

    $('.queRen').attr('style', 'display : none');
    $('.success').attr('style', 'display : block');

    let saveSeat = localStorage.getItem("saveSeat");
    let chooseSeat = localStorage.getItem("chooseSeat");
    chooseSeat = chooseSeat.split(',');
    if (saveSeat == null) {
        saveSeat = [];
    } else {
        saveSeat = saveSeat.split(',');
    }
    saveSeat.push(chooseSeat);
    localStorage.setItem("saveSeat", saveSeat.join(","));
    // $(".selected li").remove();
    // location.assign("../html/pay.html");
}
function center() {
    location.href = '个人中心.html';
}

function render() {
    let loginUser = JSON.parse(localStorage.loginUser);
    let chooseSeat = localStorage.getItem("chooseSeat");
    chooseSeat = chooseSeat.replaceAll(",", " ");
    $(".movieName").text(loginUser.order[0].movie);
    $(".movieTime").text(`今天${loginUser.order[0].month}月${loginUser.order[0].date}日 ${loginUser.order[0].time}`);
    $(".movieAddress").text(loginUser.order[0].opera);
    $(".movieSeat").text(chooseSeat);
    $(".price").text(loginUser.order[0].price);
    $(".startTime").text(loginUser.order[0].dateTime);
    $(".take ul li:first-child").html(`<img src="${loginUser.order[0].movieImgSrc}" alt="">`);
    $(".playTime").text(`今天 ${loginUser.order[0].month}月${loginUser.order[0].date}日 ${loginUser.order[0].time}`);
}

$(".takeTicket").click(function () {
    location.assign("../html/personalcenter.html");
});